package reduce.fz.com.reduce.custom;

import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.support.v4.view.ViewPager;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.LinearLayout;


import java.util.ArrayList;
import java.util.List;

import reduce.fz.com.reduce.R;
import reduce.fz.com.reduce.adapter.BannerPagerAdapter;
import reduce.fz.com.reduce.bean.Banner;
import reduce.fz.com.reduce.bean.BannerNode;
import reduce.fz.com.reduce.util.ReduceLog;
import reduce.fz.com.reduce.util.ScreenUtils;
import reduce.fz.com.reduce.util.ToastUtil;

/**
 * banner view 封装
 * Created by pengfeili on 15/9/12.
 */
public class BannerView extends FrameLayout implements BannerPagerAdapter.OnClickBannerListener {

    private Context mContext;
    private OnBannerViewActionListener mOnBannerViewActionListener;
    public  ViewPager mBannerViewPager;
    private BannerPagerAdapter mBannerPagerAdapter;
    private LinearLayout mLinearLayout;
    private List<View> dotViewsList;
    private int num=0;

    public BannerView(Context context) {
        super(context);
        mContext = context;
        initView();
    }

    public BannerView(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.mContext = context;
        initView();
    }

    public BannerView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        this.mContext = context;
        initView();
    }


    /**
     * 设置banner action listener
     *
     * @param onBannerViewActionListener
     */
    public void setOnBannerViewActionListener(OnBannerViewActionListener onBannerViewActionListener) {
        mOnBannerViewActionListener = onBannerViewActionListener;
    }

    /**
     * 渲染view
     *
     * @param banner
     */
    public void renderView(Banner banner) {
        calculateLayout(banner);
        resetDotView(banner);
        List<BannerNode> nodes = banner.items;
        mBannerPagerAdapter.replaceBanner(nodes);
        if (nodes != null && !nodes.isEmpty()) {
            mBannerViewPager.setCurrentItem(nodes.size() * 1000);
        } else {
            mBannerViewPager.setCurrentItem(0);
        }
    }

    public void setRepeat() {
        final Handler handler = new Handler() {
            @Override
            public void handleMessage(Message msg) {
                mBannerViewPager.setCurrentItem(0);
            }
        };
       new Thread()
        {
            @Override
            public void run() {
                handler.sendEmptyMessage(0);
                try {
                    Thread.sleep(3000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }.start();



    }




    /**
     * 初始化布局以及view
     */
    private void initView() {
        View rootView = LayoutInflater.from(mContext).inflate(R.layout.widget_banner_viewpager, this,
                true);

        mBannerViewPager = (ViewPager) rootView.findViewById(R.id.widget_banner_item_viewpager);
        mLinearLayout = (LinearLayout) rootView.findViewById(R.id.widget_banner_viewpager_dot_list);
        mBannerPagerAdapter = new BannerPagerAdapter(mContext, this);
        mBannerViewPager.setAdapter(mBannerPagerAdapter);
        dotViewsList = new ArrayList<>();
        mBannerViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
            }

            @Override
            public void onPageSelected(int position) {
                int position1 = position % dotViewsList.size();
                for (int i = 0, len = dotViewsList.size(); i < len; i++) {
                    if (i == position1) {//current
                        dotViewsList.get(i).setBackgroundResource(R.drawable.dot_item_current);
                    } else {
                        dotViewsList.get(i).setBackgroundResource(R.drawable.dot_item);
                    }
                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {
                if (mOnBannerViewActionListener != null) {
                    if (state == ViewPager.SCROLL_STATE_DRAGGING) {//在拖拽
                        mOnBannerViewActionListener.onHorizontalImageListScrollStart();
                    } else {//动作完成
                        mOnBannerViewActionListener.onHorizontalImageListScrollStop();
                    }
                }
            }
        });
    }

    @Override
    public void onClickBanner(BannerNode bannerNode) {
        if (mOnBannerViewActionListener != null) {
            mOnBannerViewActionListener.onClickBanner(bannerNode);
        }
    }

    /**
     * 计算布局宽高
     *
     * @param banner
     */
    private void calculateLayout(Banner banner) {
        int height = banner.height, width = banner.width;
        if (width <= 0) {
            width = ScreenUtils.getScreenW();
        }
        height = (int) (((float) height / width) * ScreenUtils.getScreenW());
        if (height <= 0) {
            height = getResources().getDimensionPixelSize(R.dimen.banner_default_height);
        }

        LayoutParams layoutParams = new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, height);
        mBannerViewPager.setLayoutParams(layoutParams);
    }

    /**
     * 重置dot view
     *
     * @param banner
     */
    private void resetDotView(Banner banner) {
        List<BannerNode> nodes = banner.items;
        //多个dot
        for (View v : dotViewsList) {
            try {
                mLinearLayout.removeView(v);
            } catch (Exception e) {
                ReduceLog.e(e);
            }
        }
        dotViewsList.clear();
        final int size = nodes.size();
        int wh = getResources().getDimensionPixelSize(R.dimen.dot_item);
        for (int i = 0; i < size; i++) {
            View dot = new View(mLinearLayout.getContext());
            LinearLayout.LayoutParams layoutParams1 = new LinearLayout.LayoutParams(wh, wh);
            layoutParams1.setMargins(wh / 3, 0, wh / 3, 0);
            dot.setLayoutParams(layoutParams1);
            if (i > 0) {
                dot.setBackgroundResource(R.drawable.dot_item);
            } else {
                dot.setBackgroundResource(R.drawable.dot_item_current);
            }
            mLinearLayout.addView(dot);
            dotViewsList.add(dot);
        }
        if (size < 2) {
            mLinearLayout.setVisibility(GONE);
        } else {
            mLinearLayout.setVisibility(VISIBLE);
        }
    }

    /**
     * 处理banner相关的动作
     */
    public interface OnBannerViewActionListener {
        /**
         * banner横向滑动开始
         */
        public void onHorizontalImageListScrollStart();

        /**
         * banner横向滑动结束
         */
        public void onHorizontalImageListScrollStop();

        /**
         * 点击banner node
         *
         * @param node
         */
        public void onClickBanner(BannerNode node);
    }
}
